package com.bdqn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdqn.dto.Msg;
import com.bdqn.model.CaiwuList;
import com.bdqn.model.RoleList;
import com.bdqn.model.Users;
import com.bdqn.service.RoleManageService;
import com.bdqn.service.TransactionService;
import com.bdqn.service.TransactionTypeService;
import com.bdqn.service.UserService;

@RequestMapping("/userManagement")
@Controller
public class UserManagementController {
	
	@Autowired
	private RoleManageService roleManageService;  //角色
	
	@Autowired
	private UserService userService; //登陆用户
	
	@Autowired
	private  TransactionTypeService  transactionTypeService; //财务类型
	
	@Autowired
	private TransactionService transactionService; //账户明细
	
	@RequestMapping("/role")
	public String queryRoleList(Model model ,HttpServletRequest request){
		List<Users> pageInfo = userService.queryUserList();
		List<RoleList> list=roleManageService.queryRole();
		request.getSession().setAttribute("listrole", list);
		request.getSession().setAttribute("usersguanli", pageInfo);
		return "userControl";
	} 
	
	@RequestMapping("/updateRole")
	@ResponseBody
	public Msg updateUser(Users user) {
		Msg msg=new Msg();
		boolean flag=userService.updateUser(user);
		if(!flag) {
			msg.setName("修改代理商用户信息失败!");
		}else {
			msg.setName("成功修改代理商用户信息!");
		}
		return msg;
	}
	
	
	@RequestMapping("/addUserData")
	@ResponseBody
	public Msg addUserData(Users user) {
		Msg msg=new Msg();
		boolean count=userService.addUserData(user);
		if(!count) {
			msg.setName("添加数据失败!");
		}else {
			msg.setName("添加数据成功!");
		}
		
		return msg;
	}
	
	
	@RequestMapping("/deleteUserData/{loginAccount}")
	@ResponseBody
	public Msg deleteUserData(@PathVariable("loginAccount")String loginAccount ) {
		Msg msg=new Msg();
		boolean delete=userService.deleteUserData(loginAccount);
		if(!delete) {
			msg.setName("删除数据失败!");
		}else {
			msg.setName("成功删除数据!");
		}
		
		return msg;
	}
	
	
	@RequestMapping("/advance")
	public String advance(Model model) {
		List<CaiwuList> finance=transactionTypeService.seekType();
	// 做账户明细查询
		model.addAttribute("finance",finance);
		return "advance";
	}
}

